package com.wangzhou.head.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangzhou.common.util.PageInfo;
import com.wangzhou.tour.dao.IAGoodsDAO;
import com.wangzhou.tour.dao.implay.AGoodsDAOImplay;
import com.wangzhou.tour.pojo.AGoods;

/**
 * 旅游模块的servlet
 * @author zjx
 *
 */
public class TourHeadServlet extends HttpServlet
{
	private IAGoodsDAO goodsDAO = new AGoodsDAOImplay();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String method = request.getParameter("method");
		if("goodslist".equalsIgnoreCase(method))
		{
			this.goodsList(request,response);
		}else if("main".equalsIgnoreCase(method))
		{
			this.main(request,response);
		}
	}
	
	/**
	 * 网站前台首页
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/head/main.jsp" ; 
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageSize(9);
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("status", "1");
		paramsMap.put("orderby", "viewCountdesc");
		List<AGoods> goodsList = this.goodsDAO.findCondList(pageInfo,"1", paramsMap);
		request.setAttribute("pageInfo", pageInfo);
		request.setAttribute("goodsList", goodsList);
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * 商品列表页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void goodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/head/goodsList.jsp" ; 
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		
		PageInfo pageInfo = new PageInfo();
		try
		{
			pageInfo.setPageSize(Integer.valueOf(pageSize));
			pageInfo.setCurrentPage(Integer.valueOf(currentPage));
		} catch (NumberFormatException e)
		{
			pageInfo.setCurrentPage(1);
			pageInfo.setPageSize(9);
		}
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("status", "1");
		List<AGoods> goodsList = this.goodsDAO.findCondList(pageInfo,"1", paramsMap);
		request.setAttribute("pageInfo", pageInfo);
		request.setAttribute("goodsList", goodsList);
		
		String resFlag = request.getParameter("resFlag");
		//这种写法永远不会报空指针
		if("json".equalsIgnoreCase(resFlag))
		//这种写法会报空指针
		//if(resFlag.equalsIgnoreCase("json"))
		{
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			
			/*
			 {
				"code":"0",
				"info":"成功",
				"data":
				{
					"goodsList":
					[
					 {
						 
					 }
					],
					"pageInfo":
					{
						
					}
				}
			}
			 */
			JSONObject resultJSON = new JSONObject();
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			try
			{
				if(goodsList.size() > 0 )
				{
					JSONObject dataJSON = new JSONObject();
					JSONArray goodsArr = new JSONArray();
					for (Iterator iterator = goodsList.iterator(); iterator.hasNext();)
					{
						AGoods goods = (AGoods) iterator.next();
						goodsArr.add(goods.toJSON());
					}
					dataJSON.put("goodsList", goodsArr);
					dataJSON.put("pageInfo", pageInfo.toJSON());
					resultJSON.put("data", dataJSON);
				}else
				{
					resultJSON.put("code", "1");
					resultJSON.put("info", "木有数据");
				}
			} catch (Exception e)
			{
				resultJSON.put("code", "-1");
				resultJSON.put("info", "服务器端报错;" + e.toString());
			}
			
			out.println(resultJSON);
			out.flush();
			out.close();
			return ;
		}
		request.getRequestDispatcher(url).forward(request, response);
	}
}
